local _M = { }
local str_find = string.find
local ngx_re_match = ngx.re.match

function _M.challenge_google(ua, result)
  if not str_find(ua, 'Google', 1, true) then
    return false
  end

  if str_find(ua, 'compatible; Googlebot', 1, true) then
    if str_find(ua, 'compatible; Googlebot-Mobile', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'GoogleBotMobile'
      return true
    else
      result['os'] = 'crawler'
      result['browser'] = 'GoogleBot'
      return true
    end
  end

  if str_find(ua, 'compatible; AdsBot-Google-Mobile;', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'AdsBotGoogleMobile'
    return true
  end

  if ngx_re_match(ua, [[^AdsBot-Google]], "o") then
    result['os'] = 'crawler'
    result['browser'] = 'AdsBotGoogle'
    return true
  end

  if str_find(ua, 'Googlebot-Image/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'GoogleBot'
    return true
  end

  if str_find(ua, 'Mediapartners-Google', 1, true) then
    if str_find(ua, 'compatible; Mediapartners-Google', 1, true) or ua == 'Mediapartners-Google' then
      result['os'] = 'crawler'
      result['browser'] = 'GoogleMediaPartners'
      return true
    end
  end

  if str_find(ua, 'Feedfetcher-Google;', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'GoogleFeedFetcher'
    return true
  end

  if str_find(ua, 'AppEngine-Google', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'GoogleAppEngine'
    return true
  end

  if str_find(ua, 'Google Web Preview', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'GoogleWebPreview'
    return true
  end

  return false
end


function _M.challenge_crawlers(ua, result)
  if str_find(ua, 'Yahoo', 1, true) or str_find(ua, 'help.yahoo.co.jp/help/jp/', 1, true)  or str_find(ua, 'listing.yahoo.co.jp/support/faq/', 1, true) then
    if str_find(ua, 'compatible; Yahoo! Slurp', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'YahooSlurp'
      return true
    end
    if str_find(ua, 'YahooFeedSeekerJp', 1, true) or str_find(ua, 'YahooFeedSeekerBetaJp', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'YahooJP'
      return true
    end
    if str_find(ua, 'crawler (http://listing.yahoo.co.jp/support/faq/', 1, true) or str_find(ua, 'crawler (http://help.yahoo.co.jp/help/jp/', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'YahooJP'
      return true
    end
    if str_find(ua, 'Y!J-BRZ/YATSHA crawler', 1, true) or str_find(ua, 'Y!J-BRY/YATSH crawler', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'YahooJP'
      return true
    end
    if str_find(ua, 'Yahoo Pipes', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'YahooJP'
      return true
    end
  end

  if str_find(ua, 'msnbot', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'msnbot'
    return true
  end

  if str_find(ua, 'bingbot', 1, true) then
    if str_find(ua, 'compatible; bingbot', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'msnbot'
      return true
    end
  end

  if str_find(ua, 'BingPreview', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'BingPreview'
    return true
  end

  if str_find(ua, 'Baidu', 1, true) then
    if str_find(ua, 'compatible; Baiduspider', 1, true) or str_find(ua, 'Baiduspider+', 1, true) or str_find(ua, 'Baiduspider-image+', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'Baiduspider'
      return true
    end
  end

  if str_find(ua, 'Yeti', 1, true) then
    if str_find(ua, 'http://help.naver.com/robots', 1, true) or str_find(ua, 'http://help.naver.com/support/robots.html', 1, true) or str_find(ua, 'http://naver.me/bot', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'Yeti'
      return true
    end
  end

  if str_find(ua, 'FeedBurner/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'FeedBurner'
    return true
  end

  if str_find(ua, 'facebookexternalhit', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'facebook'
    return true
  end

  if str_find(ua, 'Twitterbot/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'Twitterbot'
    return true
  end

  if str_find(ua, 'ichiro', 1, true) then
    if str_find(ua, 'http://help.goo.ne.jp/door/crawler.html', 1, true) or str_find(ua, 'compatible; ichiro/mobile goo;', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'ichiro'
      return true
    end
  end

  if str_find(ua, 'gooblogsearch/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'gooblogsearch'
    return true
  end

  if str_find(ua, 'Apple-PubSub', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'ApplePubSub'
    return true
  end

  if str_find(ua, '(www.radian6.com/crawler)', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'radian6'
    return true
  end

  if str_find(ua, 'Genieo/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'Genieo'
    return true
  end

  if str_find(ua, 'labs.topsy.com/butterfly/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'topsyButterfly'
    return true
  end

  if str_find(ua, 'rogerbot/1.0 (http://www.seomoz.org/dp/rogerbot', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'rogerbot'
    return true
  end

  if str_find(ua, 'compatible; AhrefsBot/', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'AhrefsBot'
    return true
  end

  if str_find(ua, 'livedoor FeedFetcher', 1, true) or str_find(ua, 'Fastladder FeedFetcher', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'livedoorFeedFetcher'
    return true
  end

  if str_find(ua, 'Hatena ', 1, true) then
    if str_find(ua, 'Hatena Antenna', 1, true) or str_find(ua, 'Hatena Pagetitle Agent', 1, true) or str_find(ua, 'Hatena Diary RSS', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'Hatena'
      return true
    end
  end

  if str_find(ua, 'mixi-check', 1, true) or str_find(ua, 'mixi-crawler', 1, true)  or str_find(ua, 'mixi-news-crawler', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'mixi'
    return true
  end

  if str_find(ua, 'Indy Library', 1, true) then
    if str_find(ua, 'compatible; Indy Library', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'IndyLibrary'
      return true
    end
  end

  if str_find(ua, 'trendictionbot', 1, true) then
      result['os'] = 'crawler'
      result['browser'] = 'trendictionbot'
    return true
  end

  return false
end


function _M.challenge_maybe_crawler(ua, result)
  local data = nil

  if ngx_re_match(ua, [[(bot|crawler|spider)(?:[-_ ./;@()]|$)]], "io") then
    result['os'] = 'crawler'
    result['browser'] = 'VariousCrawler'
    return true
  elseif ngx_re_match(ua, [[^(?:Rome Client |UnwindFetchor/|ia_archiver |Summify |PostRank/)]], "o") or str_find(ua, 'ASP-Ranker Feed Crawler', 1, true) then
    result['os'] = 'crawler'
    result['browser'] = 'VariousCrawler'
    return true
  elseif ngx_re_match(ua, [[(feed|web) ?parser]], "io") then
    result['os'] = 'crawler'
    result['browser'] = 'VariousCrawler'
    return true
  elseif ngx_re_match(ua, [[watch ?dog]], "io") then
    result['os'] = 'crawler'
    result['browser'] = 'VariousCrawler'
    return true
  end

  return false
end

return _M
